package com.turtle3d.gui.fractaloptions;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import com.turtle3d.ResourceManager;
import com.turtle3d.formallanguage.grammar.Grammar;
import com.turtle3d.formallanguage.symbol.SymbolFactory;
import com.turtle3d.visualizator.Visualizator;

/**
 * <p>Toolbox GUI component.</p>
 * 
* <p>This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.</p>
*/
public class FractalOptionsPanel extends javax.swing.JPanel {	
	private JTextField jTextField_Iterations;
	private JButton jButton_Set;
	
	private ParameterTableModel jTable_ParametersModel;
	private ResourceComboBoxModel jComboBox_GrammarsModel;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new FractalOptionsPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}	
	
	public FractalOptionsPanel() {
		super();
		initGUI();
	}
	
	private GenerateFractalEvent getFractalEvent(){
		GenerateFractalEvent generateFractalEvent = new GenerateFractalEvent();
		generateFractalEvent.setIterations(Integer.parseInt(jTextField_Iterations.getText()));
		lastGrammar = ResourceManager.loadGrammar(lastGrammar.getName());
		lastGrammar.define();
		generateFractalEvent.setGrammar(lastGrammar);
		generateFractalEvent.setVisualizator(getSelectedVisualizator());		
		
		return generateFractalEvent;
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(280, 336));
			GridBagLayout thisLayout = new GridBagLayout();
			thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 1.1};
			thisLayout.rowHeights = new int[] {7, 7, 7, 7, 7, 7, 7};
			thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1};
			thisLayout.columnWidths = new int[] {7, 7, 7};
			this.setLayout(thisLayout);					
			{
				jTextField_Iterations = new JTextField();
				this.add(jTextField_Iterations, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 0, 10, 0), 0, 0));
				jTextField_Iterations.setText("7");
				jTextField_Iterations.setSize(100, 20);
				jTextField_Iterations.setPreferredSize(new java.awt.Dimension(100, 20));				
				jTextField_Iterations.setMinimumSize(new java.awt.Dimension(30, 20));
			}
			{
				jButton_Set = new JButton();
				this.add(jButton_Set, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 0, 10, 0), 0, 0));
				jButton_Set.setText("Set");
				jButton_Set.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {													
						generateFractalListener.generate(getFractalEvent());
					}
				});
			}			
			{
				jTable_ParametersModel = new ParameterTableModel(SymbolFactory.getSymbolFactory().getDefaultParameters());
				jTable_Parameters = new JTable();
				jTable_Parameters.setPreferredScrollableViewportSize(new Dimension(180,200));							
				jTable_Parameters.setModel(jTable_ParametersModel);
				jTable_Parameters.getColumnModel().getColumn(1).setPreferredWidth(40);
			}
			{
				jScrollPane_Parameters = new JScrollPane(jTable_Parameters);
				this.add(jScrollPane_Parameters, new GridBagConstraints(0, 4, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jScrollPane_Parameters.setMinimumSize(new java.awt.Dimension(200, 200));
				jScrollPane_Parameters.setPreferredSize(new java.awt.Dimension(220, 200));
			}
			{
				jLabel_Iterations = new JLabel();
				this.add(jLabel_Iterations, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 0, 0));
				jLabel_Iterations.setText("Iterations:");
			}
			{
				jComboBox_GrammarsModel = new ResourceComboBoxModel(ResourceComboBoxModel.GRAMMARS);														
				jComboBox_Grammars = new JComboBox();
				this.add(jComboBox_Grammars, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 0, 0, 20), 0, 0));
				jComboBox_Grammars.setModel(jComboBox_GrammarsModel);				
				jComboBox_Grammars.setPreferredSize(new java.awt.Dimension(150, 20));				
				jComboBox_Grammars.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						initParameters();
					}
				});
			}
			{
				ComboBoxModel jComboBox_VisualizatorsModel = new ResourceComboBoxModel(ResourceComboBoxModel.VISUALIZATORS);													
				jComboBox_Visualizators = new JComboBox();
				this.add(jComboBox_Visualizators, new GridBagConstraints(1, 2, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 0, 0, 20), 0, 0));
				jComboBox_Visualizators.setModel(jComboBox_VisualizatorsModel);
				jComboBox_Visualizators.setPreferredSize(new java.awt.Dimension(150, 20));
				jComboBox_Visualizators.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						if(lastGrammar==null)
							initParameters();
						else{
						//add parameters not set by grammar but required by Visualizator 
						getSelectedVisualizator().setRequiredParameters();
						//refresh parameter table		
						jTable_ParametersModel
							.refresh(lastGrammar.getParameters());
						}
					}
				});
			}
			{
				jButton_NextIteration = new JButton();
				this.add(jButton_NextIteration, new GridBagConstraints(0, 5, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
				jButton_NextIteration.setText("Next Iteration");
				jButton_NextIteration.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						GenerateFractalEvent generateFractalEvent = getFractalEvent();
						generateFractalEvent.setNextIteration(true);
						generateFractalListener.generate(generateFractalEvent);
						//jTextField_Iterations.setText((new Integer(Integer.parseInt(jTextField_Iterations.getText())+1)).toString());
					}
				});
			}
			{
				jLabel1_Grammar = new JLabel();
				this.add(jLabel1_Grammar, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 20, 0, 0), 0, 0));
				jLabel1_Grammar.setText("Grammar:");
			}
			{
				jLabel1_Visualisator = new JLabel();
				this.add(jLabel1_Visualisator, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 20, 0, 0), 0, 0));
				jLabel1_Visualisator.setText("Visualisator:");
			}
			{
				jLabel_FractalOptions = new JLabel();
				this.add(jLabel_FractalOptions, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jLabel_FractalOptions.setText("Fractal Options");
			}

			initParameters();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private GenerateFractalListener generateFractalListener;
	private JTable jTable_Parameters;
	private JLabel jLabel1_Visualisator;
	private JLabel jLabel_FractalOptions;
	private JLabel jLabel1_Grammar;
	private JButton jButton_NextIteration;
	private JComboBox jComboBox_Visualizators;
	private JComboBox jComboBox_Grammars;
	private JLabel jLabel_Iterations;
	private JScrollPane jScrollPane_Parameters;

	public void setGenerateFractalListener(GenerateFractalListener generateFractalListener)
	{
		this.generateFractalListener = generateFractalListener;
	}
	
	private Grammar lastGrammar;
	
	private Grammar getSelectedGrammar(){
		String name = (jComboBox_Grammars.getModel().getSelectedItem()).toString();
		
		lastGrammar = ResourceManager.loadGrammar(name);
		lastGrammar.init();
		
		return lastGrammar;
					
	}
	
	private Visualizator getSelectedVisualizator(){
		String name = (jComboBox_Visualizators.getModel().getSelectedItem()).toString();
		
		return ResourceManager.initVisualizator(name);
	}
	
	private void initParameters(){
		//reset grammar's parameters
		Grammar g = getSelectedGrammar();
		//add parameters not set by grammar but required by Visualizator 
		getSelectedVisualizator().setRequiredParameters();
		//refresh parameter table		
		jTable_ParametersModel
			.refresh(g.getParameters());
	}
	
}
